<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" class="logo">
        <span>SpringMVC DEMO</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--      侧边栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath">
          <!-- 一级菜单区域 -->
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.psId">
            <!-- 一级菜单模板区-->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.psId]" class="icon"></i>
              <!-- 文本 -->
              <span>{{ item.psName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.path" v-for="subItem in item.children"
                          :key="subItem.psId" @click="saveNavState(subItem.path)">
              <template slot="title">
                <!--  图标   -->
                <i class="el-icon-menu"/>
                <!-- 文本 -->
                <span>{{ subItem.psName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--      右侧主体区域-->
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem("activePath")
  },
  name: "Home",
  data() {
    return {
      isCollapse: false,
      menuList: [],
      iconsObj: {
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '103': 'el-icon-s-tools',
        '125': 'el-icon-user-solid',
        '145': 'el-icon-s-marketing\n',
      },
      activePath: ''
    }
  },
  methods: {
    //退出登录
    logout() {
      window.sessionStorage.clear();
      //编程式导航，重定向
      this.$router.push("/login")
      this.$message.success("退出登录")
    },

    //获取所有菜单
    async getMenuList() {
      const {data: res} = await this.$http.get("/menus")
      if (res.meta.status !== 200) return this.$message.error("获取菜单列表失败");
      console.log(res);
      this.menuList = res.data
    },
    //点击按钮，切换菜单的折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    //保存导航链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }

  }
}
</script>

<style scoped lang="less">

.home-container {
  //高度拉满
  height: 100%;
}

.el-header {
  //背景颜色
  background-color: #373d41;
  //使用flex布局实现垂直居中
  display: flex;
  //左右贴边对齐
  justify-content: space-between;
  //去掉header的左边距
  padding-left: 0;
  //按钮不贴边，垂直居中
  align-items: center;
  //文本颜色
  color: #ffffff;
  //字体大小
  font-size: 20px;

  //设置字体的样式
  > div {
    display: flex;
    //垂直居中
    align-items: center;

    //让文本和左侧图片有15px的间距
    span {
      margin-left: 15px;
    }
  }

  img {
    height: 50px;
    width: 50px;
  }
}

.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.icon{
  margin-right: 10px;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;

}


</style>